package base

import (
	"encoding/gob"
	"gitee.com/chejiangyi/bsfgo/core/db"
	"gitee.com/chejiangyi/bsfgo/demo/domain"
	"gitee.com/chejiangyi/bsfgo/gin2/controller"
	"gitee.com/chejiangyi/bsfgo/gin2/session2"
	"gitee.com/chejiangyi/bsfgo/gin2/template2"
	"github.com/gin-gonic/gin"
	"net/http"
)

func init() {
	//注册session序列化
	gob.Register(SessionUser{})
}

type BaseAdminWebController struct {
	controller.BaseWebController
}

func (*BaseAdminWebController) GetConn() *db.DbConn {
	return domain.DataSourceInstance.GetConn()
}

// 自己寫的模板緩存渲染，解決模板聲明變量衝突導致渲染衝突問題;
func (m *BaseAdminWebController) Html(c *gin.Context, template string, data gin.H) {
	data["T"] = template2.BaseTemplateTool{}
	data["C"] = c
	data["content_template"] = template
	//加入session信息
	data["SessionUser"] = session2.SessionUtil.GetSessionDefault(c).Get("user")
	m.ReaderTemplate(c, template, data)
}

func (m *BaseAdminWebController) CheckUserAuth(c *gin.Context) {
	if session2.SessionUtil.GetSessionDefault(c).Get("user") == nil {
		c.Redirect(http.StatusFound, "/admin/home/index")
		c.Abort()
	}
	/*
		其他权限校验
	*/
}
